﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
namespace BatchPrintHelper
{
    public static class Helper
    {
        public static string WORD_EXTS = ".doc|.docx|.docm|.rtf";

        public static string EXCEL_EXTS = ".xls|.xlsx|.xlsm";

        public static bool IsWordDoc(string file)
        {
            return WORD_EXTS.Contains(Path.GetExtension(file).ToLower());
        }

        public static bool IsExcelDoc(string file)
        {
            return EXCEL_EXTS.Contains(Path.GetExtension(file).ToLower());
        }

        public static void MessageBoxException(Exception ex,string message)
        {
            var text =
                $@"{message}{Environment.NewLine}
Exception->Message:
{ex.Message}
Exception->StackTrack:
{ex.StackTrace}";

            MessageBox.Show(text, Const.APP_NAME);
        }

        public static void MessageBoxShow(string message)
        {
            MessageBox.Show(message, Const.APP_NAME);
        }

        private static readonly string[] conjunctions = new string[] { "在", "on" };
        /// <summary>
        /// http://codeprodotnet.blogspot.jp/2013/05/how-to-change-active-printer-to.html
        /// </summary>
        /// <param name="printerName"></param>
        /// <returns></returns>
        public static List<string> ConvertToExcelPrinterFriendlyName(string printerName)
        {

            var result = new List<string>();
            var key = Registry.CurrentUser;
            var subkey = key.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion\Devices");

            var value = subkey.GetValue(printerName);
            if (value == null) throw new Exception(string.Format("Device not found: {0}", printerName));

            var portName = value.ToString().Substring(9);  //strip away the winspool, 

            foreach(var con in conjunctions)
            {
                result.Add($"{printerName} {con} {portName}");
            }
            return result;
        }
    }
}
